package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import factory.DAOFactory;
import vo.Passenger;

public class BookServlet extends HttpServlet{
	public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException {
    	this.doPost(req,resp);
    }
    public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException {
    	req.setCharacterEncoding("utf-8");
    	String path="errors.jsp";
    	String status = req.getParameter("status");
    	if (status!=null) {
    		if("selectBySta".equals(status)) {
    			String setSta = req.getParameter("setSta");
    			String arSta = req.getParameter("arSta");
    			String tid = req.getParameter("tid");
    			try
    			{
            		/*List l = new ArrayList();
            		l = (List)DAOFactory.getBookDaoInstance().queryBySta(setSta,arSta);
            		Iterator i = l.iterator();
            		while(i.hasNext()) {
            			TicketIn t = new TicketIn();
            			t = (TicketIn)i.next();
            			System.out.println(t.getSeatN());
            		}*/
    				req.setAttribute("all", DAOFactory.getBookDaoInstance().queryBySta(tid));
    				req.setAttribute("setSta", setSta);
    				req.setAttribute("arSta", arSta);
    			}catch(Exception e) {
    				
    			}
    			if(req.getSession().getAttribute("leval")!=null) {
    				path = "ticketInfo05.jsp";
    		    }else{
    		    	path = "ticketInfo01.jsp";
    		    }
    	    } 
    		
    		if("selectByNum".equals(status)) {
    			String setSta = req.getParameter("setSta");
    			String arSta = req.getParameter("arSta");
            	try
    			{
            		/*List l = new ArrayList();
            		l = (List)DAOFactory.getBookDaoInstance().queryBySta(setSta,arSta);
            		Iterator i = l.iterator();
            		while(i.hasNext()) {
            			TicketIn t = new TicketIn();
            			t = (TicketIn)i.next();
            			System.out.println(t.getSeatN());
            		}*/
    				req.setAttribute("all", DAOFactory.getBookDaoInstance().queryByNum(setSta,arSta));
    				req.setAttribute("setSta", setSta);
    				req.setAttribute("arSta", arSta);
    			}catch(Exception e) {
    				
    			}
    			if(req.getSession().getAttribute("leval")!=null) {
    				path = "ticketInfo06.jsp";
    		    }else{
    		    	path = "ticketInfo03.jsp";
    		    }
    	    } 
    		
    		
    		if("bticket".equals(status)) {
    			boolean flag = false;
    			String name = req.getParameter("name");
    			String pit = req.getParameter("pit");
    			String pin = req.getParameter("pin");
    			String phone = req.getParameter("phone");
    			String tid = req.getParameter("tid");
    			int carr = Integer.parseInt(req.getParameter("carr"));
    			String seatn = req.getParameter("seatn");
    			int uid = (Integer)req.getSession().getAttribute("uid");
    			int pid = 0;
    			Passenger p = new Passenger();
    			p.setPName(name);
    			p.setPIdtype(pit);
    			p.setPIDnum(pin);
    			p.setPPhone(phone);
    			try {
		    		DAOFactory.getBookDaoInstance().insertP(p);	
		    	}catch(Exception e) {
		           e.printStackTrace();
		           System.out.println("插入乘客信息时出错");
		           req.setAttribute("err", "一人不能同时预订两个车次!!!");
		           req.getRequestDispatcher("Passenger.jsp").forward(req,resp);
		    	}
		    	try {
		    		pid = (int)DAOFactory.getBookDaoInstance().findPid(pin);
		    	}catch(Exception e) {
		           e.printStackTrace();
		           System.out.println("搜索乘客信息时出错");
		    	}
		    	try {
		    		DAOFactory.getBookDaoInstance().bookT(tid,carr,seatn);
		    	}catch(Exception e) {
		           e.printStackTrace();
		           System.out.println("插入车票错误");
		    	}
		    	try {
		    		DAOFactory.getBookDaoInstance().insertB(uid,pid,tid,carr,seatn);
		    		flag = true;
		    		req.setAttribute("flag", flag);
				    path="book_success.jsp";
		    	}catch(Exception e) {
		           e.printStackTrace();
		           System.out.println("预订出错");
		    	}
    		}
    		
    		if("bInfo".equals(status)) {
            	try
    			{
                    int uid = (Integer)req.getSession().getAttribute("uid");
    				req.setAttribute("all", DAOFactory.getBookDaoInstance().queryTInfo(uid));
    			}catch(Exception e) {}
    			path = "ticketInfo02.jsp";
    		}
    		
    		if("refundT".equals(status)) {
    			boolean flag = false;
    			String tid = req.getParameter("tid");
    			int carr = Integer.valueOf(req.getParameter("carr"));
    			String seatn = req.getParameter("seatn");
            	try
    			{
                     DAOFactory.getBookDaoInstance().unsubT(tid, carr, seatn);
                     flag = true;
 		    		req.setAttribute("flag", flag);
 				    path="refund_success.jsp";
    			}catch(Exception e) {}
    		}
    	}else {
    		  
    	}
    	req.getRequestDispatcher(path).forward(req,resp);
     }
}
